<?php

namespace july\julyadmin\builder\forms;

use july\julyadmin\builder\FormBase;

/**
 * 表单hidden组件
 */
class Hidden extends FormBase
{
    /**
     * 添加组件
     * @Author
     * @DateTime 2023-07-16T12:14:26+0800
     * @param    string                   $field 字段
     * @param    string                   $label 名称
     * @return this
     */
    public function __construct($field = '', $label = '')
    {
        $form        = new \stdClass();
        $form->type  = 'hidden';
        $form->field = $field;
        $form->label = $label;
        //内容
        $form->value   = '';
        $form->default = '';
        $this->form    = $form;
        return $this;
    }

    /**
     * 渲染组件
     * @Author
     * @DateTime 2023-07-16T12:22:46+0800
     * @return   array
     */
    public function render($items_field = '')
    {
        if ($items_field) {
            $this->html        = '';
            $this->script      = '';
            $this->form->value = '';
        }
        $this->form->value = $this->form->default;
        if (isset($this->data[$this->form->field]) && $this->data[$this->form->field] !== "") {
            $this->form->value = $this->data[$this->form->field];
        }

        $this->html = <<<EOT

            <input type="hidden" class="form-control" id="form-{$items_field}{$this->form->field}{$this->items_row_id_template_fun}" name="{$this->items_row_prefix}{$this->form->field}" value="{$this->form->value}"/>
EOT;

        $result = ['html' => $this->html, 'script' => $this->script];
        return $result;
    }
}
